(function(c) {
    var d = function() {},
        a = mobiscroll,
        b = a.$;
    a.util.addIcon = function(d, g) {
        var a = {},
            e = d.parent(),
            f = e.find('.mbsc-err-msg'),
            h = d.attr('data-icon-align') || 'left',
            c = d.attr('data-icon');
        b('<span class="mbsc-input-wrap"></span>').insertAfter(d).append(d);
        if (f) {
            e.find('.mbsc-input-wrap').append(f);
        }
        if (c) {
            if (c.indexOf('{') !== -1) {
                a = JSON.parse(c);
            } else {
                a[h] = c;
            }
        }
        if (c || g) {
            b.extend(a, g);
            e.addClass((a.right ? 'mbsc-ic-right ' : '') + (a.left ? ' mbsc-ic-left' : '')).find('.mbsc-input-wrap').append(a.left ? '<span class="mbsc-input-ic mbsc-left-ic mbsc-ic mbsc-ic-' + a.left + '"></span>' : '').append(a.right ? '<span class="mbsc-input-ic mbsc-right-ic mbsc-ic mbsc-ic-' + a.right + '"></span>' : '');
        }
    };
    a.classes.Progress = function(t, w, x) {
        var n, f, h, u, q, r, l, i, k, g, s, j, p, e = this;

        function v() {
            var a = m('value', i);
            if (a !== j) {
                o(a);
            }
        }

        function m(b, d) {
            var a = f.attr(b);
            return a === c || a === '' ? d : +a;
        }

        function o(a, g, b, d) {
            a = Math.min(k, Math.max(a, i));
            u.css('width', (a - i) * 100 / (k - i) + '%');
            if (b === c) {
                b = true;
            }
            if (d === c) {
                d = b;
            }
            if (a !== j || g) {
                e._display(a);
            }
            if (a !== j) {
                j = a;
                if (b) {
                    f.attr('value', j);
                }
                if (d) {
                    f.trigger('change');
                }
            }
        }
        a.classes.Base.call(this, t, w, true);
        e._onInit = d;
        e._onDestroy = d;
        e._display = function(a) {
            p = s && g.returnAffix ? s.replace(/\{value\}/, a).replace(/\{max\}/, k) : a;
            if (q) {
                q.html(p);
            }
            if (n) {
                n.html(p);
            }
        };
        e._attachChange = function() {
            f.on('change', v);
        };
        e.init = function(o) {
            var p, v, d, w;
            e._init(o);
            g = e.settings;
            f = b(t);
            w = f.parent().hasClass('mbsc-input-wrap');
            h = e._$parent = w ? h : f.parent();
            i = e._min = o.min === c ? m('min', g.min) : o.min;
            k = e._max = o.max === c ? m('max', g.max) : o.max;
            j = m('value', i);
            p = f.attr('data-val') || g.val;
            d = f.attr('data-step-labels');
            d = d ? JSON.parse(d) : g.stepLabels;
            s = f.attr('data-template') || (k == 100 && !g.template ? '{value}%' : g.template);
            if (!w) {
                if (e._wrap) {
                    a.util.addIcon(f);
                }
                h.find('.mbsc-input-wrap').append('<span class="mbsc-progress-cont"><span class="mbsc-progress-track mbsc-progress-anim"><span class="mbsc-progress-bar"></span></span></span>');
                u = e._$progress = h.find('.mbsc-progress-bar');
                r = e._$track = h.find('.mbsc-progress-track');
            } else {
                if (p) {
                    n.remove();
                    h.removeClass('mbsc-progress-value-' + (p == 'right' ? 'right' : 'left'));
                }
                if (d) {
                    b('.mbsc-progress-step-label', r).remove();
                }
            }
            if (l) {
                h.removeClass(l);
            }
            l = e._css + ' mbsc-progress-w mbsc-control-w mbsc-' + g.theme + (g.baseTheme ? ' mbsc-' + g.baseTheme : '') + (g.rtl ? ' mbsc-rtl' : ' mbsc-ltr');
            h.addClass(l);
            f.attr('min', i).attr('max', k);
            if (p) {
                n = b('<span class="mbsc-progress-value"></span>');
                h.addClass('mbsc-progress-value-' + (p == 'right' ? 'right' : 'left')).find('.mbsc-input-wrap').append(n);
            }
            if (d) {
                for (v = 0; v < d.length; ++v) {
                    r.append('<span class="mbsc-progress-step-label" style="' + (g.rtl ? 'right' : 'left') + ': ' + (d[v] - i) * 100 / (k - i) + '%" >' + d[v] + '</span>');
                }
            }
            q = b(f.attr('data-target') || g.target);
            e._onInit(o);
            if (!w) {
                e._attachChange();
            }
            e.refresh();
            e.trigger('onInit');
        };
        e.refresh = function() {
            o(m('value', i), true, false);
        };
        e.destroy = function(a) {
            e._onDestroy();
            h.find('.mbsc-progress-cont').remove();
            h.removeClass(l).find('.mbsc-input-wrap').before(f).remove();
            f.removeClass('mbsc-control').off('change', v);
            if (!a) {
                e._destroy();
            }
        };
        e.getVal = function() {
            return j;
        };
        e.setVal = function(a, b, c) {
            o(a, true, b, c);
        };
        if (!x) {
            e.init(w);
        }
    };
    a.classes.Progress.prototype = {
        _class: 'progress',
        _css: 'mbsc-progress',
        _hasTheme: true,
        _hasLang: true,
        _wrap: true,
        _defaults: {
            min: 0,
            max: 100,
            returnAffix: true
        }
    };
    a.presetShort('progress', 'Progress');
}());
